<!--
 * @Author: ZCL
 * @Date: 2020-05-19 13:59:47
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-07-14 21:49:43
-->
<template>
  <!-- 我的(部门资产)进入 领用 报修 转移 退还记录 -->
  <div class="record">
    <div>
      <van-list
        v-if="recordList.length"
        v-model="loading"
        :finished="!canLoadMore"
        finished-text="- 我是有底线的 -"
        @load="getMyAssetInfoRecord"
        :offset="num"
        >
        <van-steps
          active-color="#38f"
          direction="vertical"
          active-icon="clock-o"
          inactive-icon="clock-o"
        >
          <van-step v-for="(item,index) in recordList" :key="index">
            <div class="time">{{ item.create_time }}</div>
            <div class="time-box">

              <div class="title">
                <div>{{ item.f_name }}</div>
                <!-- 转移记录 有此字段 start-->
                <div v-if="$operate === OPERATE_TYPES.TRANSFER">
                  <div class="title-right" v-if="item.status === 1">转出</div>
                  <div class="title-left" v-if="item.status === 2">转入</div>
                </div>
                <!-- 转移记录 有此字段 end-->
              </div>

              <div>
                <div class="box" v-if="$operate !== OPERATE_TYPES.TRANSFER">
                  <p class="time-left">{{ $operate | handleGetTypeLabel }}部门:</p>
                  <p class="time-right">{{ item.dept_name || item.r_name }}</p>
                </div>
                <div class="box" v-if="$operate !== OPERATE_TYPES.TRANSFER">
                  <p class="time-left">{{ $operate | handleGetTypeLabel}}人员:</p>
                  <p class="time-right">{{ item.user_name }}</p>
                </div>

                <!-- 转移记录 有接收人员 start-->
                <div class="box" v-if="$operate === OPERATE_TYPES.TRANSFER">
                  <p class="time-left">接收部门:</p>
                  <p class="time-right">{{ item.transfer_dept_name }}</p>
                </div>
                <div class="box" v-if="$operate === OPERATE_TYPES.TRANSFER">
                  <p class="time-left">接收人员:</p>
                  <p class="time-right">{{ item.transfer_user }}</p>
                </div>
                <!-- 转移记录 有接收人员 end-->

                <div class="box">
                  <p class="time-left">{{ $operate | handleGetTypeLabel }}原因:</p>
                  <!-- 领用记录 原因-->
                  <p class="van-multi-ellipsis--l3 time-right" v-if="$operate === OPERATE_TYPES.RECEIVE">{{ item.re_reason }}</p>
                  <!-- 报修记录 原因 -->
                  <p class="van-multi-ellipsis--l3 time-right" v-if="$operate === OPERATE_TYPES.REPAIR">{{ item.rp_reason }}</p>
                  <!-- 转移记录 原因 -->
                  <p class="van-multi-ellipsis--l3 time-right" v-if="$operate === OPERATE_TYPES.TRANSFER">{{ item.ts_reason }}</p>
                  <!-- 退还记录 原因 -->
                  <p class="van-multi-ellipsis--l3 time-right" v-if="$operate === OPERATE_TYPES.RETURN">{{ item.ret_reason }}</p>
                </div>

                <div class="box" v-if="item.image && item.image.length>0">
                  <p class="time-left">图片详情:</p>
                  <div class="time-right">
                    <div>
                      <van-image
                        v-for="(item2,index2) in item.image"
                        :key="index2"
                        width="80px"
                        height="80px"
                        class="mr-10 mb-10"
                        :src="item2.ri_url"
                        @click="handleImagePreview(item2.ri_url)"
                      />
                    </div>
                  </div>
                </div>
              </div>

              <div class="box">
                <p class="time-left">资产状态:</p>
                <!--  re_other_status: 0正常(取re_status作判断) 1报修中 2转移中 3退还中 4报失中 5已转移 6已退还 7已报失
                      re_status: 1待审批 2待确认 3在用 4审批拒绝    -->
                <div class="time-right">
                  <!-- 领用记录 状态 start -->
                  <div v-if="$operate === OPERATE_TYPES.RECEIVE">
                    <van-button plain round type="primary" size="small" color="#FFA654" v-if="item.re_other_status === 0 && item.re_status === 1">待审批</van-button>
                    <van-button plain round type="primary" size="small" color="#FF3939" v-if="item.re_other_status === 0 && item.re_status === 2">待确认</van-button>
                    <van-button plain round type="primary" size="small" v-if="item.re_other_status === 0 && item.re_status === 3">在用</van-button>
                    <van-button plain round type="primary" size="small" color="#FF3939" v-if="item.re_other_status === 0 && item.re_status === 4">审批拒绝</van-button>
                    <van-button plain round type="warning" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 1">报修中</van-button>
                    <van-button plain round type="warning" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 2">转移中</van-button>
                    <van-button plain round type="warning" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 3">退还中</van-button>
                    <van-button plain round type="warning" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 4">报失中</van-button>
                    <van-button plain round type="default" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 5">已转移</van-button>
                    <van-button plain round type="default" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 6">已退还</van-button>
                    <van-button plain round type="default" size="small" v-if="item.re_other_status !== 0 && item.re_other_status === 7">已报失</van-button>
                  </div>
                  <!-- 领用记录 状态 end -->

                  <!-- 报修记录 状态 start -->
                  <!-- rp_status 状态：1待确认 2维修中 3维修完成 4已撤销 -->
                  <div class="flex" v-if="$operate === OPERATE_TYPES.REPAIR">
                    <van-button plain round type="warning" size="small" v-if="item.rp_status === 1">待确认</van-button>
                    <van-button plain round type="warning" size="small" color="#FFA654" v-if="item.rp_status === 2">维修中</van-button>
                    <van-button plain round type="primary" size="small" v-if="item.rp_status === 3">维修完成</van-button>
                    <van-button plain round type="default" size="small" v-if="item.rp_status === 4">已撤销</van-button>
                    <div class="text-box">
                      <router-link tag="div"
                        v-if="[1,2,3].includes(item.rp_status)"
                        :to="{
                          name: 'repairs-detail-form',
                          query: {
                            id: item.id,
                            f_id: item.f_id,
                            operate: $const.FORM_TYPES.REPAIR,
                            edit: EDIT_TYPES.DETAIL,
                          }
                        }">详情
                      </router-link>
                      <div class="line" v-if="[1,2].includes(item.rp_status)">|</div>
                      <div v-if="[1,2].includes(item.rp_status)" @click="handleEdit(item.id,item.f_id,'报修')">编辑</div>
                      <div class="line" v-if="[1,2].includes(item.rp_status)">|</div>
                      <div v-if="item.rp_status === 1" @click="handlecancel(item.id,'报修')">撤销</div>
                      <div class="line" v-if="[1,4].includes(item.rp_status)">|</div>
                      <div  v-if="item.rp_status === 4" @click="handleDel(item.id,'报修')">删除</div>
                    </div>
                  </div>
                  <!-- 报修记录 状态 end -->

                  <!-- 转移记录 状态 start -->
                  <!-- ts_status 状态：1待同事确认 2待管理员确认 3已确认 4已撤销 -->
                  <div class="flex" v-if="$operate === OPERATE_TYPES.TRANSFER">
                    <van-button plain round type="warning" size="small" v-if="item.status === 1 && item.ts_status === 1">待同事确认</van-button>
                    <van-button plain round type="warning" size="small" v-if="item.status === 2 && item.ts_status === 1">待您确认</van-button>
                    <van-button plain round type="primary" size="small" v-if="item.status === 2 && [2,3].includes(item.ts_status)">已确认</van-button>
                    <van-button plain round type="primary" size="small" v-if="item.status === 1 && [2,3].includes(item.ts_status)">同事已确认</van-button>
                    <van-button plain round type="warning" size="small" v-if="[1,2].includes(item.status) && item.ts_status === 2">待管理员确认</van-button>
                    <!-- <van-button plain round type="primary" size="small" v-if="item.status === 1 && item.ts_status === 3">同事已确认</van-button> -->
                    <van-button plain round type="primary" size="small" v-if="[1,2].includes(item.status) && item.ts_status === 3">管理员已确认</van-button>
                    <!-- 转入 -->
                    <!-- <van-button plain round type="warning" size="small" v-if="item.status === 2 && item.ts_status === 2">待管理员确认</van-button> -->
                    <!-- <van-button plain round type="warning" size="small" v-if="item.status === 2 && item.ts_status === 3">管理员已确认</van-button> -->
                    <!-- <van-button plain round type="primary" size="small" v-if="item.ts_status === 3">已确认 </van-button> -->
                    <!-- <van-button plain round type="default" size="small" v-if="item.status === 2">待您确认</van-button> -->
                    <van-button plain round type="default" size="small" v-if="item.ts_status === 4">已撤销</van-button>
                    <div class="text-box">
                      <div v-if="[1,2].includes(item.ts_status) && item.status === 1" @click="handlecancel(item.id,'转移')">撤销</div>
                      <!-- <div class="line" v-if="item.ts_status === 1 && item.ts_status === 2">|</div> -->
                      <div v-if="item.status === 2 && item.ts_status === 1" @click="handleConfirm(item.id)">确认</div>
                    </div>
                  </div>
                  <!-- 转移记录 状态 end -->

                  <!-- 退还记录 状态 start -->
                  <!-- ret_status 退还状态：1待管理员确认 2退还完成 3已撤销 -->
                  <div class="flex" v-if="$operate === OPERATE_TYPES.RETURN">
                    <van-button plain round type="warning" size="small" v-if="item.ret_status === 1">待管理员确认</van-button>
                    <van-button plain round type="primary" size="small" v-if="item.ret_status === 2">退还完成</van-button>
                    <van-button plain round type="default" size="small" v-if="item.ret_status === 3">已撤销 </van-button>
                    <div class="text-box">
                      <div v-if="item.ret_status === 1" @click="handlecancel(item.id,'退还')">撤销</div>
                      <div class="line" v-if="item.ret_status === 1 && item.ret_status === 3">|</div>
                      <div v-if="item.ret_status === 3" @click="handleDel(item.id,'退还')">删除</div>
                    </div>
                  </div>
                  <!-- 退还记录 状态 end -->

                </div>
              </div>

            </div>
          </van-step>
        </van-steps>
      </van-list>
      <van-empty
        v-if="!recordList.length"
        class="custom-image"
        :image="require('@/assets/icon/empty2.png')"
        description="暂无记录"
      />
    </div>
  </div>
</template>

<script>
import { OPERATE_TYPES, EDIT_TYPES, COMMON_TYPES } from '@/constant';
import { ImagePreview } from 'vant';
import titleMixins from '@/mixins/title';

export default {
  name: 'Record',
  mixins: [titleMixins],
  props: ['$operate', '$type'],
  data () {
    return {
      COMMON_TYPES,
      EDIT_TYPES,
      OPERATE_TYPES,
      type: '', // 领用 报修 转移 退还
      types: OPERATE_TYPES,
      page: 1,
      num: 10,
      loading: false, // 是否处于加载状态
      canLoadMore: true, // 是否已加载完成
      recordList: []
    };
  },
  methods: {
    // 资产-记录
    getMyAssetInfoRecord () {
      this.loading = true;
      let params = {
        page: this.page,
        num: this.num
      };
      const { $operate, $type } = this;
      // 我的-领用记录
      if ($operate === OPERATE_TYPES.RECEIVE && $type === COMMON_TYPES.MY) {
        this.$api.getReceiceMyAssetRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
      // 我的-报修记录
      } else if ($operate === OPERATE_TYPES.REPAIR && $type === COMMON_TYPES.MY) {
        this.$api.getRepairMyRepairRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
      // 我的-转移记录
      } else if ($operate === OPERATE_TYPES.TRANSFER && $type === COMMON_TYPES.MY) {
        this.$api.getTransferMyTransferRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
      // 我的-退还记录
      } else if ($operate === OPERATE_TYPES.RETURN && $type === COMMON_TYPES.MY) {
        this.$api.getReturnMyReturnRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
        // 部门-领用记录
      } else if ($operate === OPERATE_TYPES.RECEIVE && $type === COMMON_TYPES.DEPARTMENT) {
        params.dept_id = this.$route.query.dept_id;
        this.$api.getReceiceDeptAssetRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
        // 部门-报修记录
      } else if ($operate === OPERATE_TYPES.REPAIR && $type === COMMON_TYPES.DEPARTMENT) {
        params.dept_id = this.$route.query.dept_id;
        this.$api.getReturnDeptRepairRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
        // 部门-转移记录
      } else if ($operate === OPERATE_TYPES.TRANSFER && $type === COMMON_TYPES.DEPARTMENT) {
        params.dept_id = this.$route.query.dept_id;
        this.$api.getTransferDeptTransferRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
        // 部门-退还记录
      } else if ($operate === OPERATE_TYPES.RETURN && $type === COMMON_TYPES.DEPARTMENT) {
        params.dept_id = this.$route.query.dept_id;
        this.$api.getReturnDeptReturnRecord(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.recordList = [...this.recordList, ...res.result.data];
            this.totalPage = Number(res.result.last_page);
            this.page++;
            if (this.page > this.totalPage) {
              this.canLoadMore = false;
            }
          }
        });
      }
    },

    // 图片预览
    handleImagePreview (images) {
      const imgArr = [images];
      ImagePreview(imgArr);
    },

    // 撤销
    handlecancel (id, text) {
      // 报修撤销
      if (text === '报修') {
        this.$dialog.confirm({
          title: '撤销报修记录',
          message: '确定撤销当前报修申请吗?撤销后可以重新编辑、删除记录。'
        })
          .then(() => {
            let params = {
              id
            };
            this.$api.postRepairRevokeRepair(params).then(res => {
              if (res.code === 0) {
                this.$toast.success('撤销成功');
                this.reSet();
                this.getMyAssetInfoRecord();// 我的资产-记录
              }
            });
          });
        // 转移撤销
      } else if (text === '转移') {
        this.$dialog.confirm({
          title: '撤销转移记录',
          message: '确定撤销当前转移记录吗?'
        })
          .then(() => {
            let params = {
              id
            };
            this.$api.postTransferRevokeTransfer(params).then(res => {
              if (res.code === 0) {
                this.$toast.success('撤销成功');
                this.reSet();
                this.getMyAssetInfoRecord();// 我的资产-记录
              }
            });
          });
        // 退还撤销
      } else if (text === '退还') {
        this.$dialog.confirm({
          title: '撤销退还记录',
          message: '确定撤销当前退还记录吗?'
        })
          .then(() => {
            let params = {
              id
            };
            this.$api.postReturnRevokeReturn(params).then(res => {
              if (res.code === 0) {
                this.$toast.success('退还成功');
                this.reSet();
                this.getMyAssetInfoRecord();// 我的资产-记录
              }
            });
          });
      }
    },
    // 删除
    handleDel (id, text) {
      // 报修删除
      if (text === '报修') {
        this.$dialog.confirm({
          title: '删除报修记录',
          message: '确定删除当前报修申请吗?删除后将无法恢复,请谨慎操作。'
        })
          .then(() => {
            let params = {
              id
            };
            this.$api.postRepairRemove(params).then(res => {
              if (res.code === 0) {
                this.$toast.success('删除成功');
                this.reSet();
                this.getMyAssetInfoRecord();// 我的资产-记录
              }
            });
          });
        // 退还删除
      } else if (text === '退还') {
        this.$dialog.confirm({
          title: '删除退还记录',
          message: '确定删除当前退还记录吗?删除后将无法恢复,请谨慎操作。'
        })
          .then(() => {
            let params = {
              id
            };
            this.$api.postReturnRemoveReturn(params).then(res => {
              if (res.code === 0) {
                this.$toast.success('删除成功');
                this.reSet();
                this.getMyAssetInfoRecord();// 我的资产-记录
              }
            });
          });
      }
    },

    // 转移确认
    handleConfirm () {
      this.$dialog.confirm({
        title: '资产接收确认记录',
        message: '同事将资产转移给您,请您确认,确认后资产将归属在您的名下。'
      })
        .then(() => {
          let params = {
            id
          };
          this.$api.postTransferConfirmTransfer(params).then(res => {
            if (res.code === 0) {
              this.$toast.success('转移成功');
              this.reSet();
              this.getMyAssetInfoRecord();// 我的资产-记录
            }
          });
        });
    },
    // 编辑 前往编辑页
    handleEdit (id, f_id, text) {
      let query = {
        id,
        f_id,
        edit: EDIT_TYPES.EDIT
      };
      if (text === '报修' && this.$type === this.$const.COMMON_TYPES.MY) {
        query.title = '报修';
        query.type = COMMON_TYPES.MY;
        query.operate = this.$const.FORM_TYPES.REPAIR;
      } else if (text === '报修' && this.$type === this.$const.COMMON_TYPES.DEPARTMENT) {
        query.title = '报修';
        query.type = COMMON_TYPES.DEPARTMENT;
        query.operate = this.$const.FORM_TYPES.REPAIR;
      }
      this.$router.push({
        name: 'repairs-form',
        query
      });
    }
  },
  // 重置
  reset () {
    this.page = 1;
    this.recordList = [];
  },
  filters: {
    // type 转文字
    handleGetTypeLabel (type) {
      const labels = {
        [OPERATE_TYPES.RECEIVE]: '领用',
        [OPERATE_TYPES.REPAIR]: '报修',
        [OPERATE_TYPES.TRANSFER]: '转移',
        [OPERATE_TYPES.RETURN]: '退还'
      };
      return _.keys(labels).includes(type) ? labels[type] : '';
    }
  },
  created () {
    this.getMyAssetInfoRecord(); // 获取记录
  }
};
</script>

<style lang="scss" scoped>
.record {
  padding-bottom: 40px;
  background-color: #f8f9fd;
  ::v-deep.van-steps {
    background-color: #f8f9fd;
  }
  ::v-deep.van-step--vertical {
    padding-bottom: 0;
  }
  ::v-deep.van-icon {
    color: #0A8EFF !important;
    font-size: 16px;
  }
  ::v-deep.van-step__line {
    width: 1px;
  }
  .time {
    font-size: 12px;
    color: #717171;
  }
  .time-box {
    font-size: 13px;
    margin: 5px 10px 0 5px;
    padding: 12px;
    border-radius: 3px;
    background-color: #fff;
    box-shadow: 0px 0px 9px 2px rgba(221, 221, 221, 0.31);
    .box {
      display: flex;
      justify-content: flex-start;
      padding-bottom: 8px;
      .time-left {
        width: 60px;
        color: #AAAAAA;
      }
      .time-right {
        color: #2A2A2A;
        width: 232px;
      }
    }
    ::v-deep.van-multi-ellipsis--l3{
      width: 232px;
    }
    ::v-deep.van-button--small {
      height: 22px;
    }
    .title {
      font-size: 16px;
      color: #2A2A2A;
      padding: 5px 0 10px;
      margin-bottom: 15px;
      border-bottom: 1px solid #F5F5F5;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .title-right {
        background-color: #F9C621;
        width: 36px;
        height: 22px;
        line-height: 22px;
        border-radius: 3px;
        color: #fff;
        font-size: 14px;
        text-align: center;
      }
      .title-left {
        background-color: #8FD528;
        width: 36px;
        height: 22px;
        line-height: 22px;
        border-radius: 3px;
        color: #fff;
        font-size: 14px;
        text-align: center;
      }
    }
    .text-box {
      display: flex;
      color: #0A8EFF;
      .line {
        padding: 0 5px;
        color: #F2F2F2;
      }
    }
    .flex {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
  ::v-deep.van-empty__description {
    margin-top: 0;
  }
}
</style>
